package cn.vastbase.jdbc.type;

import org.junit.Test;

import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TypeTestCase {
    /* 定义连接对象 */
    static Connection conn = null;
    static String cname = "org.postgresql.Driver";
    static String url = "jdbc:postgresql://172.16.55.175:5432/zxs?currentSchema=zxs";
    static String username = "zxs";
    static String passwd = "Vastdata#0730";
    static CallableStatement cs = null;
    static Statement stmt = null;
    /* 定义结果集对象 */
    static ResultSet rs = null;


    @Test
    public void objectType() {
        try {
            cname = "cn.com.vastbase.Driver";
            url = "jdbc:vastbase://172.16.107.129:11110/ctp2?prepareThreshold=0&loggerLevel=trace";
            username = "admin";
            passwd = "admin";
            if (conn() == null)
                return;
            stmt = conn.createStatement();
            rs = stmt.executeQuery("select vb_version()");
            while (rs.next()) {
                Object obj = rs.getObject(1);
                System.out.println("vb_version[version=" + obj.getClass().getSimpleName() + "]");
                System.out.println("vb_version[version=" + obj + "]");
            }
            rs = stmt.executeQuery("select TY_SETTLEITEMCFG(BrokerID,ExchangeID,SettleManagerType,SettleManagerID,SettleManagerName,SettleManagerLevel,IsConfig,OrderNo,Memo,FunctionUrl,ListID,ResultClass,SettleManagerGroup,OperatorID,OpDate,OpTime) from t_settleitemcfg where rownum = 1");
            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();
            List<Object> list = new ArrayList<>();
            Struct obj = null;
            while (rs.next()) {
                Object object = rs.getObject(1);
                System.out.println(object.getClass().getSimpleName());
                obj = (Struct) rs.getObject(1);
            }
            System.out.println(obj);
            rs = stmt.executeQuery("select StartIndex,EndIndex,OrderColumns,OrderDirection from t_page where rownum = 1");
            Struct page = null;
            if (rs.next()) {
                page = conn.createStruct("TY_PAGE", new Object[]{
                        rs.getInt("StartIndex"),
                        rs.getInt("EndIndex"),
                        rs.getString("OrderColumns"),
                        rs.getString("OrderDirection")
                });
            }

            Object[] data = list.toArray(new Object[0]);
            Struct tySettleitemcfg = conn.createStruct("TY_SETTLEITEMCFG", data);

            CallableStatement cstmt = conn.prepareCall("{call pkgi_settlemgrt.up_checksettlemanager(?, ?, ?, ?, ?, ?, ?, ?)}");
            cstmt.setObject(1, obj);
            cstmt.setObject(2, page);
            cstmt.registerOutParameter(3, Types.STRUCT, "TY_SETTLEITEMCHECK");
            cstmt.setString(4, "0000.admin");
            cstmt.registerOutParameter(5, Types.NUMERIC); // 页码
            cstmt.registerOutParameter(6, Types.REF_CURSOR); // 游标
            cstmt.registerOutParameter(7, Types.NUMERIC); // 返回码
            cstmt.registerOutParameter(8, Types.VARCHAR);
            cstmt.execute();

            Struct settleItemCheck = (Struct) cstmt.getObject(3);
            System.out.println(Arrays.toString(settleItemCheck.getAttributes()));

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
    }


    public Connection conn() {
        try {
            Class.forName(cname);
            return conn = DriverManager.getConnection(url, username, passwd);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return null;

    }

    public void close() {
        try {
            /* 关闭结果集对象 */
            if (rs != null)
                rs.close();
        } catch (SQLException e) {
            System.out.println("[FAIL] close resultSet fail." + e.getMessage());
        }
        try {
            /* 关闭语句对象 */
            if (cs != null)
                cs.close();
        } catch (SQLException e) {
            System.out.println("[FAIL] close callableStatement fail." + e.getMessage());
        }
        try {
            /* 关闭语句对象 */
            if (stmt != null)
                stmt.close();
        } catch (SQLException e) {
            System.out.println("[FAIL] close statement fail." + e.getMessage());
        }
        try {
            if (conn != null)
                conn.close();
        } catch (SQLException e) {
            System.out.println("[FAIL] close connection fail." + e.getMessage());
        }
    }

}
